/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConnectionPanel.java
 *
 * Created on Sep 3, 2010, 7:52:57 PM
 */

package client;
import java.io.*;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
/**
 *
 * @author juan
 */
public class ConnectionPanel extends javax.swing.JPanel {

    /** Creates new form ConnectionPanel */
    public ConnectionPanel() {
        initComponents();
        connectionPanel = this;
    }
/**/
    String host = null,username = null, password = null, connType = null;
    int port = -1;
    protected void initializeMainPI() {
        new Thread(new Runnable() {
            public void run() {
                pi = new PI(host,port,username,password,connType,connectionPanel);

                if(pi.connect()) {
                    if(pi.login()) {
                        connectBtn.setText("Disconnect");
                        hostText.setEnabled(false);
                        userText.setEnabled(false);
                        portText.setEnabled(false);
                        passwordText.setEnabled(false);
                        connTypeCombo.setEnabled(false);
                        PWD();
                        LIST();
                    } else {
                        JOptionPane.showMessageDialog(null, "Wrong username or password.");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Could not establish a connection with the server.");
                }
            }
        }).start();
    }

    protected void RETR() {
        new Thread(new Runnable() {
            public void run() {
                String rwd = pi.getRemoteWorkingDir();
                Object selectedFile[] = fileList.getSelectedValues();
                for(int i=0; i<selectedFile.length; i++) {
                    if(((RemoteFile)selectedFile[i]).isDirectory)
                        break;
                    PI pi = new PI(host,port,username,password,connType,connectionPanel);

                    if(pi.connect() && pi.login()) {
                        pi.requestCWD(rwd);
                        pi.requestFileList();
                        pi.retrieveFile(selectedFile[i].toString());
                        sourceFileChooser.rescanCurrentDirectory();
                    } else {
                        JOptionPane.showMessageDialog(null, "Session with server lost.");
                    }
                }
            }
        }).start();
    }

    protected void STOR() {

        new Thread(new Runnable() {
            public void run() {
                System.out.println("toy adentro del run"+pi);
                String rwd = pi.getRemoteWorkingDir();
                File file[] = sourceFileChooser.getSelectedFiles();
                System.out.println("l="+file.length);
                for(int i=0; i<file.length; i++) {
                    System.out.println("toy adentro del for");
                    if(!file[i].isDirectory()) {
                        PI pi = new PI(host,port,username,password,connType,connectionPanel);
                        if(pi.connect() && pi.login()) {
                            System.out.println("??");
                            pi.requestCWD(rwd);
                            pi.requestPWD();
                            pi.requestFileList();
                            pi.storeFile(file[i].getName());
                            LIST();
                        } else {
                            JOptionPane.showMessageDialog(null, "Session with server lost.");
                        }
                    } else {
                        //TODO crear una carpeta remota y mandar todos los archivos adentro de esta carpeta
                    }
                }
            }
        }).start();
    }

    protected void PWD() {
        String dir = pi.requestPWD();
        remoteWorkingDirText.setText(dir);
    }

    protected void LIST() {
        fileList.setListData(pi.requestFileList().toArray());
    }

    public String getWorkingDir() {
        try {
            return sourceFileChooser.getCurrentDirectory().getCanonicalPath();
        } catch (Exception e) {
            return null;
        }
    }

        public void updateTransferCompletion(TransferData tdarg) {

        ListModel l = transferList.getModel();
        Object o[] = new Object[l.getSize()+1];
        for(int i=0; i<l.getSize();i++) {
            TransferData td = (TransferData)l.getElementAt(i);
            o[i] = td;

            if(td.equals(tdarg)) {
                td.sync(tdarg);
                //TODO esta mierda da error y no se atrapa
                if(td.getCompletion() == 100)
                    try{transferList.updateUI();}catch(Exception e){}

            }
        }
    }
    public void insertTransfer(TransferData tdarg) {
        ListModel l = transferList.getModel();
        Object o[] = new Object[l.getSize()+1];
        for(int i=0; i<l.getSize();i++) {
            TransferData td = (TransferData)l.getElementAt(i);
            o[i] = td;
        }
        o[l.getSize()] = tdarg;
        transferList.setListData(o);
    }

    public void notifyWindow(String msg) {
        JOptionPane.showMessageDialog(null,
                            msg, "Notification",
                                JOptionPane.INFORMATION_MESSAGE);
    }

    public void errorWindow(String msg) {
        JOptionPane.showMessageDialog(null,
                            msg, "Error",
                                JOptionPane.ERROR_MESSAGE);
    }

    public void refreshFileList(RemoteFile o[]) {
        fileList.setListData(o);
    }


/**/
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jSplitPane2 = new javax.swing.JSplitPane();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        transferListPane = new javax.swing.JScrollPane();
        transferList = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        connTypeCombo = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        connectBtn = new javax.swing.JButton();
        passwordText = new javax.swing.JPasswordField();
        portText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        userText = new javax.swing.JTextField();
        hostText = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        ProtocolListPane = new javax.swing.JScrollPane();
        protocolLog = new javax.swing.JTextArea();
        jSplitPane1 = new javax.swing.JSplitPane();
        localDirFrame = new javax.swing.JInternalFrame();
        refreshLocalButton = new javax.swing.JButton();
        uploadBtn = new javax.swing.JButton();
        sourceFileChooser = new javax.swing.JFileChooser();
        remoteDirFrame = new javax.swing.JInternalFrame();
        fileListPane = new javax.swing.JScrollPane();
        fileList = new javax.swing.JList();
        deleteBtn = new javax.swing.JButton();
        newFolderBtn = new javax.swing.JButton();
        downloadBtn = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        remoteWorkingDirText = new javax.swing.JTextField();
        renameRemoteButton = new javax.swing.JButton();
        refreshRemoteButton = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(2000, 2000));
        setMinimumSize(new java.awt.Dimension(1024, 768));

        jSplitPane2.setDividerLocation(100);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane2.setMinimumSize(new java.awt.Dimension(0, 0));

        jTabbedPane1.setMaximumSize(new java.awt.Dimension(32767, 100));
        jTabbedPane1.setMinimumSize(new java.awt.Dimension(0, 0));

        transferListPane.setViewportView(transferList);

        jTabbedPane1.addTab("Transfers", transferListPane);

        connTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Passive", "Active" }));

        jLabel5.setText("Connection Type:");

        connectBtn.setText("Connect");
        connectBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                connectBtnMouseReleased(evt);
            }
        });

        passwordText.setText("guest");

        portText.setText("55");

        jLabel2.setText("Port:");

        jLabel4.setText("Password:");

        userText.setText("guest");

        hostText.setText("192.168.1.4");
        hostText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hostTextActionPerformed(evt);
            }
        });

        jLabel3.setText("Username:");

        jLabel1.setText("Host:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(hostText, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addGap(6, 6, 6)
                        .addComponent(portText, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(userText, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(passwordText, 0, 0, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(connectBtn)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(connTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(465, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(hostText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(portText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(connTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(userText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(passwordText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(connectBtn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Connection", jPanel1);

        protocolLog.setColumns(20);
        protocolLog.setRows(5);
        ProtocolListPane.setViewportView(protocolLog);

        jTabbedPane1.addTab("Log", ProtocolListPane);

        jSplitPane2.setTopComponent(jTabbedPane1);

        jSplitPane1.setDividerLocation(550);
        jSplitPane1.setMinimumSize(new java.awt.Dimension(1000, 400));

        localDirFrame.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        localDirFrame.setResizable(true);
        localDirFrame.setTitle("Local Directory");
        localDirFrame.setMinimumSize(new java.awt.Dimension(0, 0));
        localDirFrame.setPreferredSize(new java.awt.Dimension(528, 413));
        localDirFrame.setVisible(true);

        refreshLocalButton.setText("Refresh");
        refreshLocalButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                refreshLocalButtonMouseReleased(evt);
            }
        });

        uploadBtn.setText("Upload");
        uploadBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                uploadBtnMouseReleased(evt);
            }
        });

        sourceFileChooser.setControlButtonsAreShown(false);
        sourceFileChooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
        sourceFileChooser.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        sourceFileChooser.setMultiSelectionEnabled(true);
        sourceFileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sourceFileChooserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout localDirFrameLayout = new javax.swing.GroupLayout(localDirFrame.getContentPane());
        localDirFrame.getContentPane().setLayout(localDirFrameLayout);
        localDirFrameLayout.setHorizontalGroup(
            localDirFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(localDirFrameLayout.createSequentialGroup()
                .addGroup(localDirFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(localDirFrameLayout.createSequentialGroup()
                        .addGap(155, 155, 155)
                        .addComponent(refreshLocalButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(uploadBtn))
                    .addGroup(localDirFrameLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(sourceFileChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)))
                .addContainerGap())
        );
        localDirFrameLayout.setVerticalGroup(
            localDirFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, localDirFrameLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sourceFileChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(localDirFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(uploadBtn)
                    .addComponent(refreshLocalButton))
                .addContainerGap())
        );

        jSplitPane1.setLeftComponent(localDirFrame);

        remoteDirFrame.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        remoteDirFrame.setResizable(true);
        remoteDirFrame.setTitle("Remote Directory");
        remoteDirFrame.setMinimumSize(new java.awt.Dimension(0, 0));
        remoteDirFrame.setPreferredSize(new java.awt.Dimension(528, 413));
        remoteDirFrame.setVisible(true);

        fileList.setMaximumSize(new java.awt.Dimension(1000, 10000));
        fileList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fileListMouseClicked(evt);
            }
        });
        fileListPane.setViewportView(fileList);

        deleteBtn.setText("Delete");
        deleteBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                deleteBtnMouseReleased(evt);
            }
        });

        newFolderBtn.setText("New Folder");
        newFolderBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                newFolderBtnMouseReleased(evt);
            }
        });

        downloadBtn.setText("Download");
        downloadBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                downloadBtnMouseReleased(evt);
            }
        });

        jLabel6.setText("Remote Dir:");

        remoteWorkingDirText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                remoteWorkingDirTextKeyReleased(evt);
            }
        });

        renameRemoteButton.setText("Rename");
        renameRemoteButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                renameRemoteButtonMouseReleased(evt);
            }
        });

        refreshRemoteButton.setText("Refresh");
        refreshRemoteButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                refreshRemoteButtonMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout remoteDirFrameLayout = new javax.swing.GroupLayout(remoteDirFrame.getContentPane());
        remoteDirFrame.getContentPane().setLayout(remoteDirFrameLayout);
        remoteDirFrameLayout.setHorizontalGroup(
            remoteDirFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(remoteDirFrameLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(remoteDirFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(fileListPane, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
                    .addGroup(remoteDirFrameLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(remoteWorkingDirText, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
                    .addGroup(remoteDirFrameLayout.createSequentialGroup()
                        .addComponent(refreshRemoteButton)
                        .addGap(12, 12, 12)
                        .addComponent(renameRemoteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newFolderBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(downloadBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteBtn)))
                .addContainerGap())
        );
        remoteDirFrameLayout.setVerticalGroup(
            remoteDirFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(remoteDirFrameLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(remoteDirFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(remoteWorkingDirText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fileListPane, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(remoteDirFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(renameRemoteButton)
                    .addComponent(newFolderBtn)
                    .addComponent(downloadBtn)
                    .addComponent(deleteBtn)
                    .addComponent(refreshRemoteButton))
                .addContainerGap())
        );

        jSplitPane1.setRightComponent(remoteDirFrame);

        jSplitPane2.setBottomComponent(jSplitPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 1024, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSplitPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 567, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(201, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void refreshLocalButtonMouseReleased(java.awt.event.MouseEvent evt) {
        sourceFileChooser.rescanCurrentDirectory();
}

    private void uploadBtnMouseReleased(java.awt.event.MouseEvent evt) {
        STOR();
}

    private void sourceFileChooserActionPerformed(java.awt.event.ActionEvent evt) {

}

    private void deleteBtnMouseReleased(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        Object rfile[] = fileList.getSelectedValues();
        for(int i=0; i<rfile.length;i++) {
            Response r = null;
            if(((RemoteFile)rfile[i]).isDirectory) {
                r = pi.requestRemoveDir(((RemoteFile)rfile[i]).getName());

            } else {
                r = pi.requestRemoveDir(((RemoteFile)rfile[i]).getName());
            }
            if (r!=null && r.code != 250)
                JOptionPane.showMessageDialog(null, r.msg);
        }
        PWD();
        LIST();
}

    private void remoteWorkingDirTextKeyReleased(java.awt.event.KeyEvent evt) {
        if(evt.getKeyChar()=='\n') {
            String rwd = remoteWorkingDirText.getText();
            pi.requestCWD(rwd);
            PWD();
            LIST();
        }
}

    private void renameRemoteButtonMouseReleased(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        String oldName = fileList.getSelectedValue().toString();
        String newName = (String)JOptionPane.showInputDialog("Enter the new name for the file: "+oldName);
        pi.requestRenameFile(oldName, newName);
        PWD();
        LIST();
}

    private void fileListMouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        if (evt.getClickCount() == 2 && !evt.isConsumed()) {
            evt.consume();
            //handle double click.
            RemoteFile dclicked = (RemoteFile)fileList.getSelectedValue();

            if(dclicked.isDirectory) {
                String path = pi.getRemoteWorkingDir() + "/" + dclicked.getName();
                pi.requestCWD(path);
                PWD();
                LIST();
            } else {
                pi.retrieveFile(dclicked.getName());
            }
        }
}

    private void refreshRemoteButtonMouseReleased(java.awt.event.MouseEvent evt) {
        PWD();
        LIST();
}

    private void newFolderBtnMouseReleased(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        String name = JOptionPane.showInputDialog("Enter the new directory name.");
        pi.requestMakeDir(name);
        LIST();
}

    private void downloadBtnMouseReleased(java.awt.event.MouseEvent evt) {
        RETR();
}

    private void connectBtnMouseReleased(java.awt.event.MouseEvent evt) {
        if(connectBtn.getText().equals("Connect")) {
            host = hostText.getText();
            port = Integer.parseInt(portText.getText());
            username = userText.getText();
            password = passwordText.getText();
            connType = connTypeCombo.getSelectedItem().toString();
            initializeMainPI();

        } else {
            pi.logout();
            connectBtn.setText("Connect");
            hostText.setEnabled(true);
            userText.setEnabled(true);
            portText.setEnabled(true);
            passwordText.setEnabled(true);
            connTypeCombo.setEnabled(true);
        }
}

    private void hostTextActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
}

    PI pi;

    ConnectionPanel connectionPanel;
    // Variables declaration - do not modify
    private javax.swing.JScrollPane ProtocolListPane;
    private javax.swing.JComboBox connTypeCombo;
    private javax.swing.JButton connectBtn;
    private javax.swing.JButton deleteBtn;
    private javax.swing.JButton downloadBtn;
    private javax.swing.JList fileList;
    private javax.swing.JScrollPane fileListPane;
    private javax.swing.JTextField hostText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JInternalFrame localDirFrame;
    private javax.swing.JButton newFolderBtn;
    private javax.swing.JPasswordField passwordText;
    private javax.swing.JTextField portText;
    public javax.swing.JTextArea protocolLog;
    private javax.swing.JButton refreshLocalButton;
    private javax.swing.JButton refreshRemoteButton;
    private javax.swing.JInternalFrame remoteDirFrame;
    private javax.swing.JTextField remoteWorkingDirText;
    private javax.swing.JButton renameRemoteButton;
    private javax.swing.JFileChooser sourceFileChooser;
    public javax.swing.JList transferList;
    private javax.swing.JScrollPane transferListPane;
    private javax.swing.JButton uploadBtn;
    private javax.swing.JTextField userText;
    // End of variables declaration

}
